package com.huawei.esdk.tp.common.local.impl.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.Holder;
import com.huawei.esdk.tp.common.local.bean.CameraControl;
import com.huawei.esdk.tp.common.local.bean.FreeBusyState;
import com.huawei.esdk.tp.common.local.bean.SiteInfo;
import com.huawei.esdk.tp.common.local.bean.TPSDKResponse;
import com.huawei.esdk.tp.common.local.constant.NativeConstant;
import com.huawei.esdk.tp.common.local.impl.autogen.SiteFreeBusyStates;
import com.huawei.esdk.tp.common.local.impl.autogen.SitesStatusMap;
import com.huawei.esdk.tp.common.local.impl.autogen.TPCommonSiteCtr;
import com.huawei.esdk.tp.common.local.impl.autogen.TPCommonSiteMgr;
import com.huawei.esdk.tp.common.local.impl.utils.ClientProvider;
import com.huawei.esdk.tp.common.local.impl.utils.ExceptionUtils;
import com.huawei.esdk.tp.common.local.site.SiteService;

public class SiteServiceImpl implements SiteService {
	private TPCommonSiteMgr tPCommonSiteMgr = ((TPCommonSiteMgr) ClientProvider
			.getClient(TPCommonSiteMgr.class));
	private TPCommonSiteCtr tPCommonSiteCtr = ((TPCommonSiteCtr) ClientProvider
			.getClient(TPCommonSiteCtr.class));
	private static SiteServiceImpl instance = null;
	
	public static synchronized SiteServiceImpl getInstance()
	{
		if (null == instance) {
			instance = new SiteServiceImpl();
		}
		return instance;
	}

	//@Override
	public TPSDKResponse<List<SiteInfo>> queryAllSites() {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<List<SiteInfo>> resp = new TPSDKResponse<List<SiteInfo>>();
		Holder<List<SiteInfo>> sitesInfo = new Holder<List<SiteInfo>>();
		try {
			tPCommonSiteMgr.queryAllSites(sdkResponse, sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	//@Override
	public TPSDKResponse<SiteInfo> querySiteByURI(String siteUri) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<SiteInfo> resp = new TPSDKResponse<SiteInfo>();
		Holder<SiteInfo> sitesInfo = new Holder<SiteInfo>();
		try {
			tPCommonSiteMgr.querySiteByURI(siteUri, sdkResponse, sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	//@Override
	public TPSDKResponse<List<SiteInfo>> querySiteByName(String siteName) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<List<SiteInfo>> resp = new TPSDKResponse<List<SiteInfo>>();
		Holder<List<SiteInfo>> sitesInfo = new Holder<List<SiteInfo>>();
		try {
			tPCommonSiteMgr.querySiteByName(siteName, sdkResponse,
					sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	//@Override
	public TPSDKResponse<Integer> querySiteFreeBusy(String siteUri) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<Integer> resp = new TPSDKResponse<Integer>();
		Holder<Integer> state = new Holder<Integer>();
		if (null == siteUri) {
			resp.setResultCode(NativeConstant.SITEURI_IS_NULL);
			return resp;
		}
		try {
			tPCommonSiteMgr.querySiteFreeBusy(siteUri, sdkResponse, state);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? state.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	//@Override
	public TPSDKResponse<Map<String, Integer>> querySitesFreeBusy(
			List<String> siteUris, Integer timeRange) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<Map<String, Integer>> resp = new TPSDKResponse<Map<String, Integer>>();
		Holder<List<SitesStatusMap>> sitesFreeBusyState = new Holder<List<SitesStatusMap>>();
		try {
			tPCommonSiteMgr.querySitesFreeBusy(siteUris, timeRange,
					sdkResponse, sitesFreeBusyState);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		if (0 == resultCode) {
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (SitesStatusMap status : sitesFreeBusyState.value) {
				map.put(status.getSiteUri(), status.getSiteStatus());
			}
			resp.setResult(map);
		}
		resp.setResultCode(resultCode);
		return resp;
	}

	//@Override
	public Integer ctrlCamera(String siteUri, CameraControl cameraControl) {
		try {
			return tPCommonSiteCtr.ctrlCamera(siteUri, cameraControl);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	//@Override
	public Integer ctrlAuxStream(String siteUri, Integer controlCode) {
		try {
			return tPCommonSiteCtr.ctrlAuxStream(siteUri, controlCode);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	//@Override
	public Integer micMuteSites(String confId, List<String> siteUris,
			Boolean isMute) {
		try {
			return tPCommonSiteCtr.micMuteSites(confId, siteUris, isMute);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	//@Override
	public Integer spkQuietSites(String confId, List<String> siteUris,
			Boolean isQuiet) {
		try {
			return tPCommonSiteCtr.spkQuietSites(confId, siteUris, isQuiet);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	//@Override
	public Integer connectSites(String confId, List<String> siteUris) {
		try {
			return tPCommonSiteCtr.connectSites(confId, siteUris);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	//@Override
	public Integer disconnectSites(String confId, List<String> siteUris) {
		try {
			return tPCommonSiteCtr.disconnectSites(confId, siteUris);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

   //@Override
   public TPSDKResponse<Map<String, List<FreeBusyState>>> querySiteStatus(List<String> siteUris, Date beginTime, Integer timeRange) 
   {
        TPSDKResponse<Map<String, List<FreeBusyState>>> resp = new TPSDKResponse<Map<String, List<FreeBusyState>>>();
        Holder<Integer> sdkResponse = new Holder<Integer>();
        Holder<List<SiteFreeBusyStates>> siteFreeBusyStates = new Holder<List<SiteFreeBusyStates>>();
        try 
        {
            tPCommonSiteMgr.querySiteStatus(siteUris, beginTime, timeRange, sdkResponse, siteFreeBusyStates);
        } 
        catch (Exception e) 
        {
            ExceptionUtils.processSoapException(resp,e);
            return resp;
        }
        Integer resultCode = sdkResponse.value;
        
        Map<String, List<FreeBusyState>> freeBusyStates = null;
        
        if(null != siteFreeBusyStates.value)
        {
            freeBusyStates = new HashMap<String, List<FreeBusyState>>();
            for(SiteFreeBusyStates state : siteFreeBusyStates.value)
            {
                if(freeBusyStates.containsKey(state.getUri()))
                {
                    freeBusyStates.get(state.getUri()).addAll(state.getStates());
                }
                else
                {
                    List<FreeBusyState> statues = new ArrayList<FreeBusyState>();
                    statues.addAll(state.getStates());
                    freeBusyStates.put(state.getUri(), statues);
                }
            }
        }
        
        resp.setResult(freeBusyStates);
        resp.setResultCode(resultCode);
        return resp;
    }
}
